package com.visikard.ui.fragment;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.RingUpMerchantEmailTask;
import com.visikard.item.ItemGiftKard;
import com.visikard.ui.R;
import com.visikard.ui.activity.RingUpRedeemActivity;
import com.visikard.ui.adapter.GiftSpecialsAdapter;
import com.visikard.utility.RoundNumber;
import com.visikard.widget.VerticalListView;

public class RedeemMerchantEmail extends BaseFragment implements ViewClickable  {
	
	private EditText edtPriceKard;
	private VerticalListView listView;
	private ImageView btnAcept;
	private RelativeLayout lySuccess, lyListSpecials;
	private TextView txtAmout;
	private TextView txtTitle;
	
	private GiftSpecialsAdapter couponAdapter;
	
	private double mAmount = 0;
	String emailorcode = "";
	
	private ArrayList<ItemGiftKard> mListgiftKards;
	
	private BaseTask mAsyncTask;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(
				R.layout.redeem_merchant_email, container, false);
		
		Bundle bundle = getArguments();
		if (bundle != null) {
			emailorcode = bundle.getString(RingUpRedeemActivity.EXT_CODE_INPUT);
		}
		
		innitControl(view);
		
		return view;
	}
	
	private void innitControl(View view){
		edtPriceKard = (EditText) view.findViewById(R.id.redeem_merchant_edt_ringup_monney);
		listView = (VerticalListView) view.findViewById(R.id.redeem_giftkard_listview);
		btnAcept = (ImageView) view.findViewById(R.id.redeem_merchant_accept_btn_ringup);
		lySuccess = (RelativeLayout) view.findViewById(R.id.ly_ringup_success);
		lyListSpecials = (RelativeLayout) view.findViewById(R.id.ly_list_specials_kard);
		txtAmout = (TextView) view.findViewById(R.id.text_ringup_amount);
		txtTitle = (TextView) view.findViewById(R.id.txt_title_ringup_amount);
	}
	
	private void showKeyboard(boolean show) {
		if (show)
			getActivity().getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		else
			getActivity().getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}
	
	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
		case R.id.ringup_email_finish:
		case R.id.email_exit_btn_ringup:
		case R.id.redeem_merchant_btn_close:
			showKeyboard(false);
			((RingUpRedeemActivity) getActivity()).finishActivity();
			break;
		case R.id.redeem_merchant_accept_btn_ringup:
			showKeyboard(false);
			if (checkInputRedeem()) {
				if (Constant.uid == -1 || Constant.loginSession == null) {
					Toast.makeText(getActivity(),
							R.string.login_lost, Toast.LENGTH_LONG)
							.show();
				} else {
					mAsyncTask = new RingUpMerchantEmailTask(mHandler);
					mAsyncTask.execute(Constant.uid, mAmount, emailorcode);
				}
			} else
				mHandler.sendEmptyMessage(UIMessage.MSG_INPUT_WRONG_PRICE);
			break;
		default:
			break;
		}
	}	
	
	private boolean checkInputRedeem() {
		double mRedeem = 0;
		
		String text = edtPriceKard.getText().toString().trim();
		if (edtPriceKard.getText().toString().trim().length() <= 0) {
			return false;
		} else {
			mRedeem = Double.parseDouble(text);
			if (text.startsWith("."))
				return false;
			else if (mRedeem < 0.01)
				return false;
		}
		mAmount = Double.parseDouble(edtPriceKard.getText().toString().trim());
		return true;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		AlertDialog mDialog;
		switch (msg.what) {
		case UIMessage.MSG_NOT_INPUT_PRICE:
			mDialog = new AlertDialog.Builder(getActivity())
					.setIcon(android.R.drawable.ic_dialog_info)
					.setMessage(R.string.ringup_input_amount)
					.setPositiveButton(R.string.literal_ok,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							}).create();

			mDialog.show();
			return true;
		case UIMessage.MSG_INPUT_WRONG_PRICE:
			mDialog = new AlertDialog.Builder(getActivity())
					.setIcon(android.R.drawable.ic_dialog_info)
					.setMessage(R.string.ringup_input_wrong_amount)
					.setPositiveButton(R.string.literal_ok,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							}).create();
			mDialog.show();
			return true;
		case UIMessage.MSG_RINGUP_SUCCESS:
			showKeyboard(false);
			edtPriceKard.setVisibility(View.GONE);
			btnAcept.setVisibility(View.GONE);
			
			mListgiftKards = (ArrayList<ItemGiftKard>)msg.obj;
			
			if(mListgiftKards != null && mListgiftKards.size() > 0){
				lySuccess.setVisibility(View.GONE);
				lyListSpecials.setVisibility(View.VISIBLE);
				String textRingUp = getString(R.string.ringup_success_info) + " ";
				textRingUp += Html.fromHtml("<font color='#bfcf31'>" + "$" + RoundNumber.roundToSecondDigit(mAmount) + "</font>") + ".\n";
				textRingUp += getString(R.string.ringup_success_recevice_specials) + " "; 
				textRingUp += Html.fromHtml("<font color='#bfcf31'>" + mListgiftKards.size() + "</font>");
				textRingUp +=  " " + getString(R.string.ringup_success_recevice_specials1);
				txtTitle.setText(textRingUp);
				if (couponAdapter == null){
					couponAdapter = new GiftSpecialsAdapter(getActivity(), mListgiftKards);
					listView.setAdapter(couponAdapter);
				}
				else
					couponAdapter.notifyDataSetChanged();
			}else{
				lySuccess.setVisibility(View.VISIBLE);
				lyListSpecials.setVisibility(View.GONE);
				txtAmout.setText("$" + RoundNumber.roundToSecondDigit(mAmount));
				
			}
			
			return true;
		case UIMessage.MSG_RINGUP_FAILED:
			mDialog = new AlertDialog.Builder(getActivity())
					.setIcon(android.R.drawable.ic_dialog_info)
					.setMessage(R.string.ringup_failed)
					.setPositiveButton(R.string.literal_ok,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							}).create();
			mDialog.show();
			return true;
		}
		return super.handleMessage(msg);
	}
	
}
